<template>
  <div class="container">
    <div class="item1"  >
      <img src="../assets/logo1.png" class = "logo" alt="" />
      <span class = "title" >玛卡巴卡实验室</span>
    </div>
      <el-menu
          v-for="(item,i) in tableData"
          :key="item.id"
          class="el-menu-demo"
          style="background: linear-gradient(to top, #294956, #6696AC);overflow-x:auto"
          :default-active="activeIndex"
          active-text-color="#ffd04b "
          text-color="#FFF ">
        <el-menu-item class = "el-menu-item"  v-if="i==tableData.length-1&&this.formMsg.role==1" index="i" @click="add">新建模板</el-menu-item>
        <el-menu-item class = "el-menu-item"  v-else index="i" @click="itemClick(item);">{{item.title}}</el-menu-item>
      </el-menu>
    <div class="item3" style="cursor: pointer">
      <el-dropdown v-if="this.form.role!=3" style="float: right;cursor: pointer">
    <span   class="login_button" style="font-weight: bold;text-align:center;font-size: 1.2vw;width:8vw;cursor: pointer">{{form.nickName}}
      <el-icon class="el-icon--right">
        <arrow-down />
      </el-icon>
    </span>
        <template  #dropdown >
          <el-dropdown-menu >
            <el-dropdown-item @click="this.dialogVisible=true">个人信息</el-dropdown-item>
            <el-dropdown-item @click="DownLine">退出登陆</el-dropdown-item>
            <el-dropdown-item v-if="this.formMsg.role==1" @click="dialog2Visible=true">修改模版</el-dropdown-item>
            <el-dropdown-item v-if="this.formMsg.role==1" @click="this.$router.push('/UserMsg')">成员管理</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
      <span v-else class="login_button" style="font-weight:bold; font-size: 1.2vw;text-align: center;width:8vw;line-height: 100%" @click="this.$router.push('/Login')">{{form.nickName}}
    </span>
    </div>
  </div>

  <el-dialog title="提示" v-model="dialogVisible" width="30%">
    <el-form :model="form" >
      <el-form-item label="用户名">
        <el-input v-model="form.username" style="width: 80%"></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="form.nickName" style="width: 80%"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" type="password" style="width: 80%" show-password></el-input>
      </el-form-item>
      <el-form-item label="年龄">
        <el-input v-model="form.age" style="width: 80%"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio v-model="form.sex" label="男">男</el-radio>
        <el-radio v-model="form.sex" label="女">女</el-radio>
        <el-radio v-model="form.sex" label="未知">未知</el-radio>
      </el-form-item>
      <el-form-item label="地址">
        <el-input type="text" v-model="form.address" style="width: 80%;"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">保存</el-button>
        </span>
    </template>
  </el-dialog>
  <el-dialog title="提示" v-model="dialog2Visible" width="30%">
    <el-select v-model="value" placeholder="请选择">
      <el-option
          v-for="(item) in tableData"
          :key="item.id"
          :label="item.title"
          :value="{value:item.id,label:item.title}">
      </el-option>
    </el-select>
    <el-button @click="DeleteMenu" style="margin-left: 1.5vw"> 删除</el-button>
    <el-form-item label="修改标题">
      <el-input type="text" v-model="formMenu.title" style="width: 50%;"></el-input>
    </el-form-item>
    <template #footer>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialog2Visible = false">取 消</el-button>
          <el-button type="primary" @click="saveTitle()">保存</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Header",
  data(){
    return{
      form:{},
      formMsg:{},
      dialogVisible:false,
      dialog2Visible:false,
      tableData:[],
      newMenu:[],
      value:'',
      formMenu:{},
      activeIndex:"1",
    }
  },
  created() {
    let str=sessionStorage.getItem("user")||"{}"
    this.formMsg=JSON.parse(str);
    request.get("/user/"+this.formMsg.id).then(res=>{
      if(res.code==='0'){
        this.formMsg=res.data;
      }
    })
    this.load();
    console.log(this.formMsg.role);
    setTimeout(() => {
      if(str=="{}"){
        this.form.nickName="登陆";
        this.form.role=3;
      }else {
        this.loadUser()
      }
    }, 200);

  },
  methods:{
    loadUser(){
      request.get("/user/username=",{
        params:{
          search:this.formMsg.username
        }
      }).then(res=>{
        this.form=res;
        console.log(res);
      })
    },
    load(){
      request.get("/menuList",{}).then(
          res=> {
            console.log(res);
            this.tableData = res;
            if(this.formMsg.role==1)
            this.tableData.push({});
          })
    },
    save(){
      request.put("/user", this.form).then(res=>{
        console.log(res);
        if(res.code=='0'){
          this.$message({
            type:"success",
            message:"修改成功"
          })
          this.form.role=this.formMsg.role;
          sessionStorage.setItem("user",JSON.stringify(this.form))
            this.load();
        }else{
          this.$message({
            type:"error",
            message:res.msg
          })
        }
      })
      this.dialogVisible=false;
    },
    DeleteMenu(){
      request.delete("/menuList/"+this.value.value).then(res=>{
        if(res.code=='0'){
          this.$message({
            type:"success",
            message:"成功"
          })
        }else{
          this.$message({
            type:"error",
            message:res.msg
          })
        }
      })
      this.dialog2Visible=false;
    },
    add(){
      this.newMenu={};
      this.newMenu.title="空";
      this.newMenu.kindId=-1;
      request.post("/menuList", this.newMenu).then(res=>{
        console.log(res);
        if(res.code=='0'){
          this.$message({
            type:"success",
            message:"修改成功"
          })
        }else{
          this.$message({
            type:"error",
            message:res.msg
          })
        }
      })
      this.load();
    },
    DownLine(){
      sessionStorage.clear();
      this.formMsg={};
      this.formMsg.role=3;
      this.form={};
      this.form.role=3;
      this.form.nickName="登陆"
      setTimeout(() => {
        this.load();
      }, 1000);
    },
    saveTitle(){
      this.formMenu.id=this.value.value;
      if(this.formMenu.title==null){
        this.formMenu.title="空"
      }
      request.put("/menuList",this.formMenu).then(res=>{
        if(res.code=='0'){
          this.$message({
            type:"success",
            message:"成功"
          })
        }else{
          this.$message({
            type:"error",
            message:res.msg
          })
        }
      })
      this.dialog2Visible=false;
    },
    itemClick(item){
      switch (item.kindId){
        case -1:this.$router.push({path:'/StyleChange',query: {pageId:item.id}});break;
        case 0:this.$router.push({path:'/',query: {pageId:item.id}});break;
        case 1:this.$router.push({path:'/WebPage1',query: {pageId:item.id}});break;
        case 2:this.$router.push({path:'/WebPage2',query: {pageId:item.id}});break;
        case 3:this.$router.push({path:'/WebPage3',query: {pageId:item.id}});break;
        case 4:this.$router.push({path:'/WebPage4',query: {pageId:item.id,blockId:0,pageNum:1}});break;
      }
    }
  },


}
</script>

<style scoped>
.container
{
  position: fixed;
  top:0;
  margin-top: 0vw;
  width: 100%;
  display: flex;
  display: -webkit-flex; /* Safari */
  background:linear-gradient(to top, #294956, #6696AC);
  height:4vw;
  z-index: 9;
}

.logo {
  position: absolute;
  width: 3.5vw;
  height: 3vw;
  top: 0.5vw;
  left: 1vw;

}
.title{
  position: absolute;
  font-family: 方正粗黑宋简体;
  color: white;
  width: 16vw;
  height: 4vw;
  padding-top: 0.5vw;
  left: 5vw;
  text-align: center;
  font-size: 2vw;
}
.el-menu-demo {

  background-image: linear-gradient(to top, #294956, #6696AC);
  position: relative;
  right: 8vw;
  text-align: center;
  height: 4vw;
}
.el-menu-item{
  margin-top: 0vw;
  display: flex;
  justify-content: center;
  text-align: center;
  font-size:1.3vw;
  height: 4vw;
  background-color: #545c64;
  background-image: linear-gradient(to top, #294956, #6696AC);

}

.el-menu-demo :hover{
  color: #F0FFF0;
  background-color: #545c64;
  background-image: linear-gradient(to top, #294956, #6696AC);
  font-size: 1.7vw;
}

.item1
{
  flex:2;
}
.item2
{
}
.item3
{

}
.login_button{
  position : absolute;
  right: 0;
  top: 1.4vw;
  color: #FFFFFF;
}
</style>
